<script setup>
import {ref} from "vue";
import {showFailToast, showSuccessToast} from "vant";
import router from "../modules/router.js";
import {setResetToken, setToken} from "@/utils/auth.js";
import {tokenLogin, tokenRefresh} from "@/api/login.js";
import {setLocalStorage} from "@/utils/localStorage.js";

const username = ref('');
const password = ref('');
const onSubmit = () => {
  const values = username.value
    console.log('----',values)
    if (values && values != ''){
      tokenLogin(values).then(res=>{

        tokenRefresh(values).then(res=>{
          setToken(res.result.newToken)
          setResetToken(res.result.newToken)
          router.push({
            path: '/',
            query: {
              id: 1
            }
          })
        }).finally(()=>{

        })
      })


    }

};

const cancel = ()=>{
  router.push('/')
}
</script>

<template>
    <div class="loginBox">
        <van-form @submit="onSubmit" label-align="top" style="width: 90%;">
            <van-cell-group inset>
                <van-field
                    v-model="username"
                    name="密钥"
                    label="密钥"
                    placeholder="密钥"
                    type="textarea"
                    rows="3"
                    :rules="[{ required: true, message: '请输入密钥' }]"
                />
            </van-cell-group>
            <div style="margin: 16px;">
                <van-button round block type="primary" native-type="submit">
                    登录
                </van-button>
              <van-button round block type="default" @click="cancel" class="!mt-2" >
                取消
              </van-button>
            </div>
        </van-form>
    </div>
</template>

<style scoped>
.loginBox {
    width: 100%;
    height: 100vh;
    overflow: hidden;
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: #f2f5f9;
}
</style>
